import axios from '../axios'
export default {
    namespaced: true,
    state: {
        customers: [],
        pageSize: 3,
        cutPage: 1,
        total: "",
        totalPage: '',
        type: 'name',
        value: ''
    },
    mutations: {
        setCustomers(state, data) {
            state.customers = data.allCustomers
            state.total = data.total
            state.totalPage = data.totalPage
        },
        setPageSize(state, newPageSize) {
            state.pageSize = newPageSize
        },
        setCutPage(state, newCutPage) {
            state.cutPage = newCutPage
        },
        setType(state, newType) {
            state.type = newType
        },
        setValue(state, newValue) {
            state.value = newValue
        }
    },
    actions: {
        async addCustomers(context, newInf) {
            const { data } = await axios({
                url: '/customers/addCustomers',
                method: 'post',
                data: newInf
            })
            alert(data.message)
        },
        async getCustomers(context, newInf) {
            const { data } = await axios({
                url: '/customers/getCustomers',
                method: 'get',
                params: {
                    pageSize: context.state.pageSize,
                    cutPage: context.state.cutPage,
                    type: context.state.type,
                    value: context.state.value
                }
            })
            context.commit('setCustomers', data)
        },
        async delCustomers(context, _id) {
            const { data } = await axios({
                url: '/customers/delCustomers',
                method: 'post',
                data: { _id }
            })
            alert(data.message);
        },
        async getCustomerBySalesMen(context, salesMenId) {
            const { data } = await axios({
                url: '/customers/getCustomerBySalesMen',
                method: 'get',
                params: {
                    salesMenId,
                    pageSize: context.state.pageSize,
                    cutPage: context.state.cutPage,
                    type: context.state.type,
                    value: context.state.value
                }
            })
            context.commit('setCustomers', data)
        },
        async changeCustomers(context, newInf) {
            const { data } = await axios({
                url: '/customers/changeCustomers',
                method: 'post',
                data: newInf
            })
            alert(data.message);
        }
    }
}